{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这是一种非常特殊的思路,它代表了传统机器学习和深度学习的一种结合思想."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "\n",
    "class WideAndDeepModel(nn.Module):\n",
    "    def __init__(self, wide_input_dim, deep_input_dim, hidden_dim, output_dim):\n",
    "        super(WideAndDeepModel, self).__init__()\n",
    "        # 定义Wide部分\n",
    "        self.linear = nn.Linear(wide_input_dim, output_dim)\n",
    "        \n",
    "        # 定义Deep部分\n",
    "        self.deep = nn.Sequential(\n",
    "            nn.Linear(deep_input_dim, hidden_dim),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(hidden_dim, hidden_dim),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(hidden_dim, output_dim)\n",
    "        )\n",
    "    \n",
    "    def forward(self, wide_inputs, deep_inputs):\n",
    "        # Wide部分\n",
    "        wide_out = self.linear(wide_inputs)\n",
    "        \n",
    "        # Deep部分\n",
    "        deep_out = self.deep(deep_inputs)\n",
    "        \n",
    "        # 结合Wide和Deep部分的结果\n",
    "        combined_out = wide_out + deep_out\n",
    "        \n",
    "        return combined_out\n",
    "\n",
    "# 假设的输入尺寸\n",
    "wide_input_dim = 10  # 假设Wide部分的输入特征维度\n",
    "deep_input_dim = 50  # 假设Deep部分的输入特征维度\n",
    "hidden_dim = 64      # 隐藏层维度\n",
    "output_dim = 1       # 输出维度（二分类问题）\n",
    "\n",
    "# 创建模型实例\n",
    "model = WideAndDeepModel(wide_input_dim, deep_input_dim, hidden_dim, output_dim)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "结合了传统机器学习的逻辑回归Wide Component"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestRegressor\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "\n",
    "class WideAndDeepModel(nn.Module):\n",
    "    def __init__(self, wide_model, deep_input_dim, hidden_dim, output_dim):\n",
    "        super(WideAndDeepModel, self).__init__()\n",
    "        # 定义Wide部分\n",
    "        self.wide_model = wide_model  # 这里可以是RF、SVM等传统模型\n",
    "        \n",
    "        # 定义Deep部分\n",
    "        self.deep = nn.Sequential(\n",
    "            nn.Linear(deep_input_dim, hidden_dim),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(hidden_dim, hidden_dim),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(hidden_dim, output_dim)\n",
    "        )\n",
    "    \n",
    "    def forward(self, wide_inputs, deep_inputs):\n",
    "        # Wide部分\n",
    "        wide_out = torch.tensor(self.wide_model.predict(wide_inputs), dtype=torch.float32)\n",
    "        \n",
    "        # Deep部分\n",
    "        deep_out = self.deep(deep_inputs)\n",
    "        \n",
    "        # 结合Wide和Deep部分的结果\n",
    "        combined_out = wide_out + deep_out\n",
    "        \n",
    "        return combined_out\n",
    "\n",
    "# 假设的输入尺寸\n",
    "wide_input_dim = 10  # 假设Wide部分的输入特征维度\n",
    "deep_input_dim = 50  # 假设Deep部分的输入特征维度\n",
    "hidden_dim = 64      # 隐藏层维度\n",
    "output_dim = 1       # 输出维度（二分类问题）\n",
    "\n",
    "# 创建Wide部分的模型（这里以随机森林为例）\n",
    "wide_model = RandomForestRegressor(n_estimators=100, random_state=42)\n",
    "\n",
    "# 创建模型实例\n",
    "model = WideAndDeepModel(wide_model, deep_input_dim, hidden_dim, output_dim)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
